#ifndef TRANSPLOT_H
#define TRANSPLOT_H

#include <qwt_plot.h>
#include <QPointF>
#include <QMap>
#include <qwt_scale_widget.h>
#include <qwt_scale_draw.h>
#include <qwt_plot_grid.h>

#include "transseries.h"
#include "TransMatch.h"
#include "scrollzoomer.h"

#define HEIGH_SERIES 3

class Zoomer: public ScrollZoomer
{
public:
    Zoomer( QWidget *canvas ):
        ScrollZoomer( canvas )
    {
#if 0
        setRubberBandPen( QPen( Qt::red, 2, Qt::DotLine ) );
#else
        setRubberBandPen( QPen( Qt::red ) );
#endif
    }

    virtual QwtText trackerTextF( const QPointF &pos ) const
    {
        QColor bg( Qt::white );

        QwtText text = QwtPlotZoomer::trackerTextF( pos );
        text.setBackgroundBrush( QBrush( bg ) );
        return text;
    }

    virtual void rescale()
    {
        QwtScaleWidget *scaleWidget = plot()->axisWidget( yAxis() );
        QwtScaleDraw *sd = scaleWidget->scaleDraw();

        double minExtent = 0.0;
        if ( zoomRectIndex() > 0 )
        {
            // When scrolling in vertical direction
            // the plot is jumping in horizontal direction
            // because of the different widths of the labels
            // So we better use a fixed extent.

            minExtent = sd->spacing() + sd->maxTickLength() + 1;
            minExtent += sd->labelSize(
                scaleWidget->font(), 1000 ).width();
        }

        sd->setMinimumExtent( minExtent );

        ScrollZoomer::rescale();
    }
};

class TransPlot : public QwtPlot
{
public:
    typedef QMap<QString, TransSeries*> type_series;
    explicit TransPlot(const QwtText& title,  QWidget * parent);
    virtual ~TransPlot();

    void addTransaction(const Transaction& transaction);

    virtual void drawCanvas(QPainter * painter);

    const TransSeries* findSeriesByName(const QString& Name, bool& found) const;

    // Return True means successful grant control of lock. False means cannot get lock.
    bool getLock() const { return !trans_lock; }
    void Lock() { trans_lock = true; }
    void unLock() { trans_lock = false; }

    /// Functions do with Lock
    void updateDrawRect(const QRectF& DrawF) { drawRectOfTransPlot = DrawF; }
    void updateDrawRect(const QPointF& Point) { drawRectOfTransPlot = QRectF(Point.x(), Point.y(), drawRectOfTransPlot.width(), drawRectOfTransPlot.height()); }
    void resetDrawRect() { drawRectOfTransPlot = defaultRectOfTransPlot; }

private:
    QMap<QString, TransSeries*> map_series;
    JCBMatch jcbmatch;
    /// Height for each series
    int heigh_series_next;
    unsigned int width_max;
    int color_id;
    /// True means Transplot is locked. False means Transplot can be used.
    bool trans_lock;

    QRectF drawRectOfTransPlot;
    QRectF defaultRectOfTransPlot;
    QwtPlotGrid *grid;
    Zoomer* zoomer;

    bool isDefaultRectUpdated;
    int defaultTransPlotRectWidth;
    int defaultTransPlotRectHeigh;

    /// Draw a transaction series
    void drawTransSeries(QPainter* painter, const TransSeries* series) const;

    /// Draw a transaction
    void drawTransaction(QPainter* painter, const Transaction& transaction, int heigh, const QColor& color, const bool skipContent) const;

    /// Draw content in a transcation
    void drawColum(QPainter* painter, const Transaction& transaction, const QPoint& lefttop, int heigh) const;

    /// Draw the Rect of a transaction
    int drawTransRect(QPainter* painter, const QPoint& lefttop, const Transaction& Transaction, const QColor& color, const bool skipContent) const;

    /// For connection analysis
    void anaylsisConnectionAndDraw(QPainter* painter, const QString SeriesMaster, const QString SeriesSlave, TransMatch* match);
    void drawConnectionLine(QPainter* painter, const Transaction* trans1, const int heigh1, const Transaction* trans2, const int heigh2);

    bool isTransactionInDrawRect(const Transaction& trans, unsigned int heigh) const;

    void updateDefaultRectFrame(QPainter* painter);

};

#endif // TRANSPLOT_H
